﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Intellego.Web.ValidationEngine;

namespace MIAPWEB.Forms.Controles
{
    public partial class wucAgregaVariablesPaquete : UserControl
    {

        public List<string> ListaVariables
        {
            get
            {
                if (ViewState["ListaVariables"] == null)
                {
                    ViewState["ListaVariables"] = new List<string>();
                }

                return (List<string>)ViewState["ListaVariables"];
            }
            set { ViewState["ListaVariables"] = value; }
        }       

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            this.AddControls();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AddControls();
            }
            this.InitValidation();
        }

        /// <summary>
        /// Establece los parametros obligatorios en la captura
        /// </summary>
        private void InitValidation()
        {
            var errNotEmpty = this.GetGlobalResourceObject("Strings", "ErrorRequerido").ToString();
            var reglas = new List<ValidationRule>();

            foreach (Control ctl in DynamicControlsHolder.Controls)
            {
                if (!(ctl is TextBox)) continue;
                var txt = ctl as TextBox;
                var ruleEmpty = new ValidationRule(txt);
                ruleEmpty.NotEmpty(errNotEmpty);
                reglas.Add(ruleEmpty);
            }

            var masterPage = this.Page.Master;

            if (masterPage != null)
            {
                var parent = masterPage.Page as PageBase;

                if (parent != null)
                {
                    foreach (var validationRule in reglas)
                    {
                        parent.ValidationEngine.Add(validationRule);
                    }

                }
            }
        }


        /// <summary>
        /// agrega los controles al sistema
        /// </summary>
        private void AddControls()        
        {
            this.DynamicControlsHolder.Controls.Clear();
            int index = 1;
            foreach (var nombreVar in ListaVariables)
            {                
                var txt = new TextBox {ID = "txt" + index, Text = nombreVar.Trim()};
                var btn = new Button { ID = "btn" + index, Text = @"Eliminar", CausesValidation = false};
                btn.Click += btn_Click;
                DynamicControlsHolder.Controls.Add(new LiteralControl("<div >"));
                DynamicControlsHolder.Controls.Add(txt);
                DynamicControlsHolder.Controls.Add(btn);
                DynamicControlsHolder.Controls.Add(new LiteralControl("</div>"));
                DynamicControlsHolder.Controls.Add(new LiteralControl("<hr>"));
                index++;
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            this.GetValores();
            this.GetValores();
            var btn =(Button) sender;
            var index = Convert.ToInt32(btn.ID.Replace("btn", string.Empty)) - 1;
            this.ListaVariables.RemoveAt(index);
            this.AddControls(this.ListaVariables);
        }

        protected void BtnAgregar_OnClick(object sender, EventArgs e)
        {
            this.GetValores();
            ListaVariables.Add(string.Empty);
            this.AddControls(this.ListaVariables);
        }

        /// <summary>
        /// Agrega los controles a la pantalla segun las variables
        /// </summary>
        /// <param name="listaVariables"></param>
        /// <param name="variablesValores"></param>
        public void AddControls(List<string> listaVariables)
        {
            this.ListaVariables = listaVariables;
            this.DynamicControlsHolder.Controls.Clear();
            this.AddControls();
        }

        /// <summary>
        /// obtiene los valores de los textBox
        /// </summary>
        /// <returns></returns>
        public List< string> GetValores()
        {

            var regreso = new List<string>();
            foreach (Control ctl in DynamicControlsHolder.Controls)
            {
                if (!(ctl is TextBox)) continue;
                var txt = ctl as TextBox;
                regreso.Add( txt.Text);
            }
            this.ListaVariables = regreso;
            return this.ListaVariables;
        }

        public void Clean()
        {
            this.ListaVariables = null;
            this.AddControls(this.ListaVariables);
        }
    }
}